/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import bookmaker.HibernateUtil;
import javax.faces.context.FacesContext;

/**
 *
 * @author Maciek
 */
public class Transact {

    private long id;
    private String type;
    private double credit;
    private Bettor bettor;
    private Session session;


    public String deposit() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            this.type = "+";
            this.bettor = (Bettor)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bettor");
            this.bettor.deposit(credit);
            getSession().save(this);
            getSession().update(this.bettor);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return "ok";
    }

     public String withdraw() {
        this.bettor = (Bettor)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bettor");
        if(credit>bettor.getBalance()){
            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("param", "WithdrawalError");
            return "withdrawalError";
        }
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            setSession(sessionFactory.openSession());
            Transaction tx = getSession().beginTransaction();
            this.type = "-";
            
            this.bettor.withdraw(credit);
            getSession().save(this);
            getSession().update(this.bettor);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            getSession().close();
        }
        return "ok";
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the credit
     */
    public double getCredit() {
        return credit;
    }

    /**
     * @param credit the credit to set
     */
    public void setCredit(double credit) {
        this.credit = credit;
    }

    /**
     * @return the bettor
     */
    public Bettor getBettor() {
        return bettor;
    }

    /**
     * @param bettor the bettor to set
     */
    public void setBettor(Bettor bettor) {
        this.bettor = bettor;
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

}
